package com.yzly.demo.controller;

import com.alibaba.fastjson.JSONObject;
import com.yzly.demo.utils.RequestGetJsonBody;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 同程异步推送接口
 */
@RestController
@Api(value = "TCPushController", description = "同程推送接口相关AIP", tags = "TCPushController")
public class TCPushController {

    private Logger logger = LoggerFactory.getLogger(TCPushController.class);

    @PostMapping("/bookSeatResult")
    @ApiOperation(value = "占座结果推送", httpMethod = "POST")
    public JSONObject bookSeatResult(@RequestBody JSONObject data) {
        //获取推送的内容
//        JSONObject data = RequestGetJsonBody.getJsonByRequest(req);

        //根据推送内容进行相对应操作
        if (data != null) {
            if ("100".equals(data.get("msgCode"))) {
                logger.info("干得漂亮，占座成功！");
            } else {
                logger.info("搞毛线，占座失败！原因：" + data.get("msgInfo"));
            }
        }


        //告诉同程服务器我们成功收到消息
        return successGetPushMessage();
    }

    @PostMapping("/bookAndIssueOrderResult")
    @ApiOperation(value = "占座&出票结果合并结果推送", httpMethod = "POST")
    public JSONObject bookAndIssueOrderResult(HttpServletRequest req) {
        //获取推送的内容
        JSONObject data = RequestGetJsonBody.getJsonByRequest(req);
        //记录推送日志

        //根据推送内容进行相对应操作
        if ("100".equals(data.get("msgCode"))) {
            logger.info("成功！");
        } else {
            logger.info("失败！原因：" + data.get("msgInfo"));
        }

        //告诉同程服务器我们成功收到消息
        return successGetPushMessage();
    }

    @PostMapping("/notifyOvertimeOrder")
    @ApiOperation(value = "订单过期推送", httpMethod = "POST")
    public JSONObject notifyOvertimeOrder(HttpServletRequest req) {
        //获取推送的内容
        JSONObject data = RequestGetJsonBody.getJsonByRequest(req);
        //记录推送日志

        //根据推送内容进行相对应操作
        if ("100".equals(data.get("msgCode"))) {
            logger.info("成功！");
        } else {
            logger.info("失败！原因：" + data.get("msgInfo"));
        }

        //告诉同程服务器我们成功收到消息
        return successGetPushMessage();
    }

    @PostMapping("/refundTicket")
    @ApiOperation(value = "退款结果推送", httpMethod = "POST")
    public JSONObject refundTicket(HttpServletRequest req) {
        //获取推送的内容
        JSONObject data = RequestGetJsonBody.getJsonByRequest(req);
        //记录推送日志

        //根据推送内容进行相对应操作
        if ("100".equals(data.get("msgCode"))) {
            logger.info("成功！");
        } else {
            logger.info("失败！原因：" + data.get("msgInfo"));
        }

        //告诉同程服务器我们成功收到消息
        return successGetPushMessage();
    }

    @PostMapping("/cancelOrder")
    @ApiOperation(value = "取消结果推送", httpMethod = "POST")
    public JSONObject cancelOrder(HttpServletRequest req) {
        //获取推送的内容
        JSONObject data = RequestGetJsonBody.getJsonByRequest(req);
        //记录推送日志

        //根据推送内容进行相对应操作
        if ("100".equals(data.get("msgCode"))) {
            logger.info("成功！");
        } else {
            logger.info("失败！原因：" + data.get("msgInfo"));
        }

        //告诉同程服务器我们成功收到消息
        return successGetPushMessage();
    }

    @PostMapping("/issueOrderResult")
    @ApiOperation(value = "出票结果推送", httpMethod = "POST")
    public JSONObject issueResult(HttpServletRequest req) {
        //获取推送的内容
        JSONObject data = RequestGetJsonBody.getJsonByRequest(req);
        //记录推送日志

        //根据推送内容进行相对应操作
        if ("100".equals(data.get("msgCode"))) {
            logger.info("成功！");
        } else {
            logger.info("失败！原因：" + data.get("msgInfo"));
        }

        //告诉同程服务器我们成功收到消息
        return successGetPushMessage();
    }


    /**
     * 成功获取同程推送消息答复
     *
     * @return
     */
    private JSONObject successGetPushMessage() {
        JSONObject json = new JSONObject();
        json.put("resCode", 100);
        json.put("resMsg", "成功");
        return json;
    }
}
